package com.zzuli.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzuli.practice.entity.Order;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.mapper.OrderMapper;
import com.zzuli.practice.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    public IPage<Order> pageOrderList(Integer pageIndex){
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        // 首先需要定义一个拦截器才能使用
        Page<Order> page = new Page<>(pageIndex, 5);

        IPage<Order> iPage = orderMapper.selectPage(page, null);
        // 判断当前页是否大于总页数
        if (iPage.getCurrent() > iPage.getPages()){
            page.setCurrent(iPage.getPages());
        }
        return orderMapper.selectPage(page, null);
    }

    public int insertOrder(Order order){
        return orderMapper.insert(order);
    }

    public int updateForStatusById(Integer id, Integer status, String repairmanName, Integer repairmanId){
        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        wrapper.set("repairman", repairmanName);
        wrapper.set("repairmanid", repairmanId);
        return orderMapper.update(null, wrapper);
    }

    public Order selectById(Integer id){
        return orderMapper.selectById(id);
    }

    public int deleteOrderById(Integer id){

        // 逻辑删除
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return orderMapper.delete(wrapper);
    }

}
